package cn.openea.perpartner.payment.web.wallet.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.PageDomain;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.payment.web.wallet.dto.SearchDto;
import cn.openea.perpartner.payment.web.wallet.dto.WalletDto;
import cn.openea.perpartner.payment.web.wallet.entity.Bills;
import cn.openea.perpartner.payment.web.wallet.entity.Wallet;
import cn.openea.perpartner.payment.web.wallet.service.IBillsService;
import cn.openea.perpartner.payment.web.wallet.service.IWalletService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Weird077
 * @description TODO
 * @date 2022-03-13 23:20
 */
@Api(tags = "我的钱包")
@RestController
@RequestMapping("/wallet")
public class WalletController extends BaseController {
    @Autowired
    private IWalletService walletService;
    @Autowired
    private IBillsService billsService;

    @ApiOperation("查询余额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id")
    })
    @PostMapping("/getCoins")
    public AjaxResult selectCoins(@RequestBody WalletDto dto) {
        Wallet wallet = walletService.queryUserCoin(dto);
        String coin = BeanUtil.isNotEmpty(wallet) ? wallet.getCoin() : "0";
        return AjaxResult.success("操作成功！", coin);
    }

    @ApiOperation("余额提现")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "coin", value = "提现金额"),
            @ApiImplicitParam(name = "orderId", value = "订单编号")
    })
    @PostMapping("/cashOut")
    public AjaxResult cashOut(@RequestBody WalletDto dto) {
        if(!walletService.hasCashOut(dto.getUserId())){
            return error("提现功能受限，请联系客服。");
        }
        if(!walletService.cashOut(dto)){
            return error("操作失败，请检查钱包余额是否充足！");
        };
        return success() ;
    }
    @ApiOperation("余额扣减")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "coin", value = "扣减金额"),
            @ApiImplicitParam(name = "orderId", value = "订单编号"),
            @ApiImplicitParam(name = "reason", value = "扣减原因")
    })
    @PostMapping("/deduct")
    public AjaxResult deductBalance(@RequestBody WalletDto dto) {
        if(!walletService.deductBalance(dto)){
            return error("操作失败，请检查钱包余额是否充足！");
        };
        return success() ;
    }
    @ApiOperation("现金充值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "coin", value = "充值金额"),
            @ApiImplicitParam(name = "orderId", value = "订单编号")
    })
    @PostMapping("/recharge")
    public AjaxResult recharge(@RequestBody WalletDto dto) {
        boolean flag = walletService.recharge(dto);
        return flag ? success() : error();
    }
    @ApiOperation("现金补发")
    @PostMapping("/repair")
    public AjaxResult repair(@RequestBody WalletDto dto) {
        boolean flag = walletService.repair(dto);
        return flag ? success() : error();
    }

    @ApiOperation("积分转入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "points", value = "所需积分"),
            @ApiImplicitParam(name = "orderId", value = "订单编号")
    })
    @PostMapping("/toWallet")
    public AjaxResult toWallet(@RequestBody WalletDto dto) {
        dto.setReason("积分转入");
        boolean flag = walletService.pointsToWallet(dto);
        return flag ? success() : error("操作失败，请检查可用积分是否充足！");
    }

    @ApiOperation("兑换积分-按金额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "coin", value = "兑换金额"),
            @ApiImplicitParam(name = "orderId", value = "订单编号")
    })
    @PostMapping("/toPoint")
    public AjaxResult toPoint(@RequestBody WalletDto dto) {
        boolean flag = walletService.walletToPoint(dto);
        return flag ? success() : error("操作失败，您的钱包余额不足！");
    }

    @ApiOperation("兑换积分-按积分")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "points", value = "兑换积分"),
            @ApiImplicitParam(name = "orderId", value = "订单编号")
    })
    @PostMapping("/toPoints")
    public AjaxResult toPoints(@RequestBody WalletDto dto) {
        boolean flag = walletService.walletToPoints(dto);
        return flag ? success() : error("操作失败，您的钱包余额不足！");
    }

    @ApiOperation("全部账单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "pageNum", value = "当前页数"),
            @ApiImplicitParam(name = "pageSize", value = "页显示数")
    })
    @PostMapping("/userBillsList")
    public TableDataInfo selectUserBil(@RequestBody WalletDto dto) {
        startPage();
        List<Bills> list = billsService.selectList(dto);
        return getDataTable(list);
    }

    @ApiOperation("入账详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "pageNum", value = "当前页数"),
            @ApiImplicitParam(name = "pageSize", value = "页显示数")
    })
    @PostMapping("/add/list")
    public TableDataInfo selectAddList(@RequestBody WalletDto dto) {
        startPage();
        dto.setOperType("add");
        List<Bills> list = billsService.selectList(dto);
        return getDataTable(list);
    }

    @ApiOperation("支出详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "pageNum", value = "当前页数"),
            @ApiImplicitParam(name = "pageSize", value = "页显示数")
    })
    @PostMapping("/sub/list")
    public TableDataInfo selectSubList(@RequestBody WalletDto dto) {
        startPage();
        dto.setOperType("sub");
        List<Bills> list = billsService.selectList(dto);
        return getDataTable(list);
    }

    @ApiOperation("所有账单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数"),
            @ApiImplicitParam(name = "pageSize", value = "页显示数")
    })
    @PostMapping("/all/list")
    public TableDataInfo selectBillsList(@RequestBody PageDomain page) {
        startPage();
        List<Bills> list = billsService.selectList();
        return getDataTable(list);
    }

    @ApiOperation("所有钱包")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数"),
            @ApiImplicitParam(name = "pageSize", value = "页显示数")
    })
    @PostMapping("/walletList")
    public TableDataInfo selectWalletList(@RequestBody PageDomain page) {
        startPage();
        List<Wallet> list = walletService.queryWalletList();
        return getDataTable(list);
    }

    @ApiOperation("账单查询")
    @PostMapping("/bills/List")
    public TableDataInfo selectListByAccount(@RequestBody SearchDto dto) {
        startPage();
        List<Bills> list = billsService.selectListByAccount(dto);
        return getDataTable(list);
    }
    @ApiOperation("账单已读")
    @PostMapping("/bills/mark/read")
    public AjaxResult markBillsRead(@RequestBody SearchDto dto) {

        return  billsService.markBillsRead(dto)?success():error();
    }

    @ApiOperation("补发账单")
    @PostMapping("/bills/repair")
    public AjaxResult selectById(@RequestBody WalletDto dto) {
        Bills bills = billsService.getById(dto.getUnid());
        return success(bills);
    }
    @ApiOperation("清空余额")
    @PostMapping("/empty")
    public AjaxResult emptyWallet(@RequestBody WalletDto dto) {

        return toAjax(walletService.emptyWallet(dto.getUserId()));
    }

}
